package com.banzhi.shop.fragment;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.CommodityDetailsActivity;
import com.banzhi.shop.activity.CommodityDetails_DZZQActivity;
import com.banzhi.shop.entity.GoodsDetails.GoodsDetails_Data;
import com.banzhi.shop.model.CallBack.CommodityDetailsCallBack;
import com.banzhi.shop.model.ICommodityDetailsModel;
import com.banzhi.shop.model.Impl.CommodityDetailsImpl;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

/**
 * 商品详情页里的商品介绍
 */

public class GoodsIntroductionFrg extends Fragment implements CommodityDetailsCallBack {
    public static final String TAG = "GoodsIntroductionFrg";
    private View view;
    private WebView webView;
    private ICommodityDetailsModel model;
    private Activity activity;
    private String GoodID;
    /**
     * 商品详情数据实体类
     */
    private GoodsDetails_Data goodsdata;
    private FrameLayout mFrameLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg_goos_introduction, container, false);
        initView();
        model = new CommodityDetailsImpl();

        activity = getActivity();
        if (activity instanceof CommodityDetailsActivity) {
            GoodID = ((CommodityDetailsActivity) activity).getGoodID();
        }else if(activity instanceof CommodityDetails_DZZQActivity){
            GoodID = ((CommodityDetails_DZZQActivity) activity).getGoodID();
        }
        model.getData(GoodID, "0", this);
        return view;
    }

    @Override
    public void onPause() {
        webView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        webView.destroy();
        super.onDestroy();
    }

    private MyWebChromeClient mMyWebChromeClient;

    private void initView() {
        webView = (WebView) view.findViewById(R.id.webview);
        mFrameLayout = (FrameLayout) view.findViewById(R.id.mFrameLayout);
        getActivity().getWindow().setFlags(//强制打开GPU渲染
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        WebSettings settings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        //允许与js交互
        settings.setJavaScriptEnabled(true);
        //设置webview的插件转状态
        settings.setPluginState(WebSettings.PluginState.ON);
        //webview的缓存模式
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //自动加载图片
        settings.setLoadsImagesAutomatically(true);
        settings.setBuiltInZoomControls(true);
        //自动缩放
        settings.setSupportZoom(true);

        mMyWebChromeClient = new MyWebChromeClient();
        webView.setWebChromeClient(mMyWebChromeClient);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
//        //允许与js交互
//        settings.setJavaScriptEnabled(true);
//        //设置webview的插件转状态
//        settings.setPluginState(WebSettings.PluginState.ON);
//        //webview的缓存模式
//        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
//        //自动加载图片
//        settings.setLoadsImagesAutomatically(true);
//        settings.setBuiltInZoomControls(true);
//        //自动缩放
//        settings.setSupportZoom(true);
//        webView.setWebChromeClient(new WebChromeClient());
        //webveiw加载网页的方式
        //        webView.loadDataWithBaseURL();
        //        webView.loadData();
////        webView.loadUrl(URL);
//
////        settings.setUseWideViewPort(true);
////        settings.setLoadWithOverviewMode(true);
////        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

    }

    /**
     * 展示数据
     */
    private void showWebview() {
        String str = goodsdata.getContent();
//        webView.loadUrl(str);

        webView.loadDataWithBaseURL(null, str, "text/html", "UTF-8", null);
        LogUtils.d("链接", str);
    }

    /**
     * @param object 获取商品介绍成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        goodsdata = (GoodsDetails_Data) object;
        showWebview();
    }


    /**
     * @param object 获取商品介绍失败
     */
    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }


    public class MyWebChromeClient extends WebChromeClient {
        private View mCustomView;
        private CustomViewCallback mCustomViewCallback;

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }
            mCustomView = view;
            mFrameLayout.addView(mCustomView);
            mCustomViewCallback = callback;
            webView.setVisibility(View.GONE);
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        public void onHideCustomView() {
            webView.setVisibility(View.VISIBLE);
            if (mCustomView == null) {
                return;
            }
            mCustomView.setVisibility(View.GONE);
            mFrameLayout.removeView(mCustomView);
            mCustomViewCallback.onCustomViewHidden();
            mCustomView = null;
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            super.onHideCustomView();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        switch (config.orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                break;
        }
    }
}